package cloudspace.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public abstract class ClassFinder
{
    public static class DirectoryResources
    {
        private List<File> sources = new ArrayList<File>();
        private List<File> classes = new ArrayList<File>();
        private List<File> libraries = new ArrayList<File>();
        private long classesModTime = 0;
        private long librariesModTime = 0;
        public void addSource( File file )
        {
            if(classesModTime < file.lastModified())
                classesModTime = file.lastModified();
            sources.add( file );   
        }

        public void addClass( File file )
        {
            if(librariesModTime < file.lastModified())
                librariesModTime = file.lastModified();
            classes.add( file );
        }
        public boolean haveCompiledFilesChanged(long since)
        {
            //There are no files detected.  Just notify to wipe the directory.
            if(classesModTime == 0 && librariesModTime == 0)
            {
                return true;
            }
            //Have the classes been modified since the last time we checked?
            if(classesModTime != 0 && since <=classesModTime)
            {
                return true;
            }
            //Have the libraries been modified since the last time we checked?
            if(librariesModTime != 0 && since <= librariesModTime)
            {
                return true;
            }
            return false;
        }
        public void addLibrary( File file )
        {
            libraries.add( file );
        }
        public List<File> getSources()
        {
            return sources;
        }
        public List<File> getClasses()
        {
            return classes;
        }
        public List<File> getLibraries()
        {
            return libraries;
        }
        
    }
    public static DirectoryResources buildSource(File startDir, String excludeDir)
    {
        return buildSource(startDir.listFiles(),new DirectoryResources(),excludeDir);
    }
    public static DirectoryResources buildSource(File startDir)
    {
        return buildSource(startDir.listFiles(),new DirectoryResources(),null);
    }
    private static DirectoryResources buildSource(
        File[] files,
        DirectoryResources content,String excludeDir )
    {
        if(files.length == 0)
            return content;
        for ( int i = 0; i < files.length; i++ )
        {
            if(excludeDir != null && files[i].isDirectory() && files[i].getName().equals( excludeDir ))
                continue;
            if ( files[i].isDirectory())
            {
                buildSource( files[i].listFiles(), content, excludeDir );
            }
            if ( files[i].getName().endsWith( ".java" ) )
                content.addSource( files[i] );
            if ( files[i].getName().endsWith( ".class" ))
                content.addClass(files[i]);
            if ( files[i].getName().endsWith( ".jar" ) )
                content.addLibrary(files[i]);
        }
        return content;
    }
}
